package com.lynn.springboot_demo.utils;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author cl
 * @Date 2020/6/19 09:19
 * MD5加密算法
 */
public class MD5Utils {

    private static final String MY_STR = "9572";

    /**
     * MD532位小写字母加密
     *
     * @param encryptStr 待加密字符串
     * @return
     */
    public static String encryptMD5(String encryptStr) {

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update((MY_STR + encryptStr).getBytes());
            String string = new BigInteger(1, md.digest()).toString(16);
            return getStr(string);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getStr(String string) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < (string.length()) / 4; i++) {
            String substring = string.substring(i * 4, i * 4 + 4);
            if (i != (string.length() / 4) - 1) {
                str.append(substring).append("-");
            } else {
                str.append(substring);
            }
        }
        return String.valueOf(str);
    }

    public static void main(String[] args) {
        //9dc1-1ba4-bf6c-f5d2-77f4-419f-46b0-3a04
        String s = encryptMD5("123");
        System.out.println(s);

    }
}
